#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
#include"cxcore.h"





int main()
{
    IplImage* img1=cvLoadImage("left.png",CV_LOAD_IMAGE_GRAYSCALE);
    IplImage* img2=cvLoadImage("right.png",CV_LOAD_IMAGE_GRAYSCALE);


cvSmooth(img1, img1, CV_GAUSSIAN,3,3,0,0);
    cvSmooth(img2,img2,CV_GAUSSIAN,3,3,0,0);
    cvNamedWindow("D1", CV_WINDOW_AUTOSIZE);
    cvShowImage("left",img1);

    CvMat* disparity_left  = cvCreateMat(img1->height, img1->width, CV_16S);
CvMat* disparity_right = cvCreateMat(img2->height, img2->width, CV_16S);
CvStereoBMState* state = cvCreateStereoBMState(0,256);
//cvFindStereoCorrespondenceBM(img1,disparity_left,disparity_right,state);
//cvFindStereoCorrespondenceBM(img2,disparity_left,disparity_right,state);

cvFindStereoCorrespondenceBM(img1,img2,disparity_left,state);
CvMat* disp_left_visual = cvCreateMat(img1->height, img1->width, CV_8U);
cvConvertScale( disparity_left, disp_left_visual, 20 ,0);
cvSave( "disparity.pgm", disp_left_visual,0,0,cvAttrList(0,0) );
cvThreshold(disp_left_visual,img1,120,120,CV_THRESH_BINARY);
cvNamedWindow("Disparity map", CV_WINDOW_AUTOSIZE);
cvShowImage("Disparity map", disp_left_visual);
cvWaitKey(0);
    //cvNamedWindow("left",CV_WINDOW_AUTOSIZE);
    //cvNamedWindow("right",CV_WINDOW_AUTOSIZE);
    //cvShowImage("left",img1);
    //cvShowImage("right",img2);
   // cvWaitKey(0);
return 0;
}
